/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUIs;
import Core.ConnectionToDatabase;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
 *
 * @author jc156563
 */
class EditInventoryItemAmountUI extends Frame implements ActionListener, ListSelectionListener
{
    protected JList inventory;
        protected JTextField newName;
        protected JFrame frame;

    public void show()
    {
        frame = new JFrame("Edit Item Quantity");
        JPanel panel = new JPanel();
        JButton confirm = new JButton("Confirm");
        JButton back = new JButton("Back");
        newName = new JTextField(20);
        ConnectionToDatabase con = new ConnectionToDatabase();
        Vector<String> inventoryData = new Vector<String>();
        inventoryData.addAll(con.getInventoryNames());

        inventory = new JList(inventoryData);

        panel.add(inventory);
        panel.add(newName);
        panel.add(confirm);
        panel.add(back);

        confirm.addActionListener(this);
        confirm.setActionCommand("confirm");
        back.addActionListener(this);
        back.setActionCommand("back");
        inventory.addListSelectionListener(this);


        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        String action;
        action = e.getActionCommand();

        if (action.equals("confirm"))
        {


            if (!inventory.isSelectionEmpty())
            {
                ConnectionToDatabase con = new ConnectionToDatabase();
                if(con.editItemQuantity(inventory.getSelectedValue().toString(), newName.getText()))
                {
                    JOptionPane.showMessageDialog(frame, "Item Quantity Was Successfully Changed");
                    frame.setVisible( false );
                    frame.dispose();
                    EditInventoryItemAmountUI newWindow = new EditInventoryItemAmountUI();
                    newWindow.show();
                }
                else
                {
                    JOptionPane.showMessageDialog(frame, "Item Quantity Was Not Successfully Changed");
                }
            }
            else
            {
                JOptionPane.showMessageDialog(frame, "Please make a selection");
            }
        }
        else if (action.equals("back"))
        {
            MainUI main = new MainUI();
            main.show();
            frame.setVisible( false );
            frame.dispose();
        }


    }

    public void valueChanged(ListSelectionEvent e)
    {
        ConnectionToDatabase con = new ConnectionToDatabase();

        if (e.getValueIsAdjusting() == true)
        {
            newName.setText(con.getInventoryQuantity(inventory.getSelectedValue().toString()));
        }
    }

}
